package com.apes.scm.warning;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.account.account.repository.StockRepository;
import com.apes.scm.contract.repository.ContractRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("stockWarningService")
public class StockWarningService extends DomainService {

    @Autowired
    private StockRepository stockRepository;

    /** 库存数量预警汇总 */
    public List<Map> stockQtyWarning(SimpleRequest request) {
        return stockRepository.stockWarning();
    }

    /** 库存数量预警明细 */
    public Page<Object> stockQtyWarningItem(SimpleRequest request) {
        List<Map> stockWarnings = stockRepository.stockWarningItem(JSONObject.parseObject(request.getData().toString()).getJSONObject("data").getString("LEFTTEXT"));
        Pageable pageable = PageRequest.of(((Integer) request.get("page")) - 1, request.get("size"));
        if (stockWarnings.size() != 0) {
            int total = Integer.valueOf(stockWarnings.get(0).get("TOTAL").toString());
            PageImpl<Object> pPage = new PageImpl(stockWarnings, pageable, stockWarnings.size() == 0 ? 0 : total);
            return pPage;
        }
        return new PageImpl(stockWarnings, pageable, stockWarnings.size() == 0 ? 0 : 0);
    }

    /** 对账SQL语句 */
    public List<Map> getStockWarningItemSql(SimpleRequest request) {
        return stockRepository.getStockWarningItemSql(request.get("LEFTTEXT"));
    }
}
